#!/bin/bash

Com_echo_info()
{
    echo -e "\033[40;36m$1\033[0m"
}

Com_echo_warn()
{
    echo -e "\033[40;33m$1\033[0m"
}

Com_echo_err()
{
    echo -e "\033[40;31m$1\033[0m"
}

g_tool_flag_file="/tmp/first_setup_tool"
g_pro_src_path=$(pwd)

if [ ! -d "$g_pro_src_path/build" ];then
    mkdir build
    chmod 777 build -R
fi

if [ ! -d "$g_pro_src_path/program" ];then
    mkdir program
    chmod 777 program -R
fi

cd ../
g_tool_path=`pwd`
cat /etc/profile | grep "$g_tool_path/toolchains/arm-anycloud-linux-uclibcgnueabi/bin"
if [ $? -eq 0 ];then
    Com_echo_info "toolchains exist"

    if [ -e "$g_tool_flag_file" ];then
        source /etc/profile
    fi
else
    Com_echo_warn "setup toolchains"
    echo -e "export PATH=$g_tool_path/toolchains/arm-anycloud-linux-uclibcgnueabi/bin:\$PATH" >> /etc/profile
    echo -e "export LD_LIBRARY_PATH=$g_tool_path/toolchains/arm-anycloud-linux-uclibcgnueabi/lib:\$LD_LIBRARY_PATH" >> /etc/profile
    source /etc/profile
    touch $g_tool_flag_file
fi
cd -

rm $g_pro_src_path/build/* -rf
cd build
cmake -Wno-dev -DCMAKE_TOOLCHAIN_FILE=AnyKaEV300L.cmake ..
make -j4 #VERBOSE=1

chmod 777 bin/*
cp bin/* ../program
